//
//  NetworkingTools.swift
//  NBblock
//
//  Created by apple on 15/9/25.
//  Copyright © 2015年 cc. All rights reserved.
//
/*
App Key：1902764114
App Secret：18a23e78ad155111fa25e093fe2f38f0
https://api.weibo.com/oauth2/authorize?client_id=1902764114&redirect_uri=http://www.baidu.com
*/

import UIKit
import AFNetworking
// 请求方法枚举类型
enum RequestMethod: String {
    case GET = "GET"
    case POST = "POST"
}



class NetworkingTools: AFHTTPSessionManager {
    
    // MARK: - 应用信息
    let appKey = "1902764114"
    let appSecret = "18a23e78ad155111fa25e093fe2f38f0"
    let redirectURL = "http://www.baidu.com"
    
    /// - see: [http://open.weibo.com/wiki/Oauth2/authorize](http://open.weibo.com/wiki/Oauth2/authorize) 拼接授权页面url
    var oauthURL: NSURL {
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(appKey)&redirect_uri=\(redirectURL)"
        
        return NSURL(string: urlString)!
    }
    
    // 创建网络工具单例
    static let sharedTools: NetworkingTools = {
        let st = NetworkingTools(baseURL: nil)
        // 设置反序列化的格式
        st.responseSerializer.acceptableContentTypes?.insert("text/html")
        
        return st
    }()
    
    // MARK: - 加载 AccessToken
    
    
    // MARK: - 加载用户信息
    
    // MARK: - 封装网络工具类 GET && POST
    func request(requestMethod: RequestMethod!, URLString: String, parameters: [String : AnyObject]?, finished:(result: AnyObject?, error: NSError?) -> ()) {
        
        if requestMethod == RequestMethod.GET {
            GET(URLString, parameters: parameters, success: { (_, result) -> Void in
//                print("\(result)")
                finished(result: result, error: nil)
                
                }) { (_, error) -> Void in
                    
                    finished(result: nil, error: error)
            }
        }else
        {
            POST(URLString, parameters: parameters, success: { (_, result) -> Void in
                
                finished(result: result, error: nil)
                
                }, failure: { (_, error) -> Void in
                    
                    finished(result: nil, error: error)
            })
        }
        
    }
         
}
